import 'dart:io';

import 'package:flutter/material.dart';
import 'package:citiesocial/configs/configs.dart';
import 'package:citiesocial/_include.dart';
import 'package:citiesocial/ui/goods/search_goods.dart';

///appbar回调更新
typedef UpdateUICallback();

///appbar工具类
class AppBarUtils {
  static final AppBarUtils _singleton = AppBarUtils._internal();
  AppBarUtils._internal();
  factory AppBarUtils() => _singleton;

  ///统一的图标间距
  static const inner_image_padding = const EdgeInsets.all(13.0);

  ///统一的中间的图标间距
  static const inner_image_title_padding = const EdgeInsets.all(15.0);

  ///统一的appBar x轴高度（阴影大小）
  static const appbar_elevation = 0.5;

  ///统一的title字体大小
  static const title_font_size = 17.0;

  ///统一的menu字体大小
  static const menu_font_size = 15.0;

  /// 带搜索图标
  static AppBar withSearch(BuildContext context, {String hint = '搜索'}) =>
      AppBar(
        elevation: appbar_elevation,
        titleSpacing: 0.0,
        brightness: Brightness.light,
        backgroundColor: Colors.white,
        leading: GestureDetector(
          onTap: () {
            Navigator.pushNamed(context, '/message');
          },
          child: Container(
            padding: inner_image_padding,
            child: Image.asset(
              'imgs/js_navbars_img_btn_livechat_n.png',
              fit: BoxFit.scaleDown,
            ),
          ),
        ),
        title: GestureDetector(
          onTap: () {
            Navigator.push(context,
                MaterialPageRoute(builder: (context) => SearchGoods()));
          },
          child: Container(
            height: double.infinity,
            child: Row(
              mainAxisAlignment: MainAxisAlignment.center,
              crossAxisAlignment: CrossAxisAlignment.center,
              children: <Widget>[
                Container(
                  margin: EdgeInsets.only(right: 5.0),
                  child: Image.asset(
                    'imgs/js_cells_img_search.png',
                    fit: BoxFit.scaleDown,
                    height: 20.0,
                  ),
                ),
                Text(
                  '$hint',
                  style:
                      TextStyle(color: Colors.grey, fontSize: title_font_size),
                ),
              ],
            ),
            margin: EdgeInsets.symmetric(vertical: 10.0),
            decoration: ShapeDecoration(
              shape: RoundedRectangleBorder(
                borderRadius: BorderRadius.all(
                  Radius.circular(10.0),
                ),
              ),
              color: Configs.background,
            ),
          ),
        ),
        actions: <Widget>[
          GestureDetector(
            onTap: () {
              Navigator.pushNamed(context, '/cart');
            },
            child: Container(
              padding: inner_image_padding,
              child: Image.asset(
                Configs.cartCount == 0
                    ? 'imgs/js_navbars_img_shoppingcart_empty.png'
                    : 'imgs/js_navbars_img_shoppingcart_exist.png',
                fit: BoxFit.scaleDown,
              ),
            ),
          ),
        ],
      );

  /// 带购物车图标
  static AppBar withCart(BuildContext context, {String title = ''}) => AppBar(
        elevation: appbar_elevation,
        titleSpacing: 0.0,
        brightness: Brightness.light,
        backgroundColor: Colors.white,
        leading: GestureDetector(
          onTap: () {
            Navigator.maybePop(context);
          },
          child: Container(
            padding: inner_image_padding,
            child: Image.asset(
              'imgs/js_alerts_img_back.png',
              fit: BoxFit.scaleDown,
            ),
          ),
        ),
        title: Text(
          '$title',
          style: TextStyle(color: Colors.black, fontSize: title_font_size),
        ),
        actions: <Widget>[
          GestureDetector(
            onTap: () {
              Navigator.pushNamed(context, '/cart');
            },
            child: Container(
              padding: inner_image_padding,
              child: Image.asset(
                Configs.cartCount == 0
                    ? 'imgs/js_navbars_img_shoppingcart_empty.png'
                    : 'imgs/js_navbars_img_shoppingcart_exist.png',
                fit: BoxFit.scaleDown,
              ),
            ),
          ),
        ],
      );

  /// 商品详情页，带购物车图标和收藏图标
  static AppBar detail(BuildContext context,
          {GoodsEntity goods, UpdateUICallback callback}) =>
      AppBar(
        elevation: appbar_elevation,
        titleSpacing: 0.0,
        brightness: Brightness.light,
        backgroundColor: Colors.white,
        leading: GestureDetector(
          onTap: () {
            Navigator.maybePop(context);
          },
          child: Container(
            padding: inner_image_padding,
            child: Image.asset(
              'imgs/js_alerts_img_back.png',
              fit: BoxFit.scaleDown,
            ),
          ),
        ),
        title: Text(
          '${goods?.goods_name}',
          style: TextStyle(color: Colors.black, fontSize: title_font_size),
        ),
        actions: <Widget>[
          GestureDetector(
            onTap: () async {
              Configs.mayLogin(context).then((yes) async {
                if (yes) {
                  if ('1' == goods?.is_focus) {
                    bool remove = await HttpManager.deleteFocus(
                        goods.goods_id, Configs.userId, Configs.userToken,
                        context: context);
                    if (remove) {
                      goods.is_focus = '0';
                      //Toast.show(context, '取消收藏成功');
                      if (callback != null) callback();
                      // } else {
                      //   Toast.show(context, '取消收藏失败');
                    }
                  } else {
                    bool add = await HttpManager.addFocus(
                        goods.goods_id, Configs.userId, Configs.userToken,
                        context: context);
                    if (add) {
                      goods.is_focus = '1';
                      //Toast.show(context, '添加收藏成功');
                      if (callback != null) callback();
                      // } else {
                      //   Toast.show(context, '添加收藏失败');
                    }
                  }
                }
              });
            },
            child: Container(
              padding: inner_image_padding,
              child: Image.asset(
                '1' == goods?.is_focus
                    ? 'imgs/js_navbars_img_fav_green_h.png'
                    : 'imgs/js_navbars_img_fav_green_n.png',
                fit: BoxFit.scaleDown,
              ),
            ),
          ),
          GestureDetector(
            onTap: () {
              Navigator.pushNamed(context, '/cart');
            },
            child: Container(
              padding: inner_image_padding,
              child: Image.asset(
                Configs.cartCount == 0
                    ? 'imgs/js_navbars_img_shoppingcart_empty.png'
                    : 'imgs/js_navbars_img_shoppingcart_exist.png',
                fit: BoxFit.scaleDown,
              ),
            ),
          ),
        ],
      );

  /// 普通的只有一个返回和标题的
  static AppBar normal(BuildContext context, String title, [Widget menu]) =>
      AppBar(
        elevation: appbar_elevation,
        titleSpacing: 0.0,
        brightness: Brightness.light,
        backgroundColor: Colors.white,
        leading: GestureDetector(
          onTap: () {
            Navigator.maybePop(context);
          },
          child: Container(
            padding: inner_image_padding,
            child: Image.asset(
              'imgs/js_alerts_img_back.png',
              fit: BoxFit.scaleDown,
            ),
          ),
        ),
        title: Text(
          '$title',
          style: TextStyle(color: Colors.black, fontSize: title_font_size),
        ),
        actions: menu == null ? const <Widget>[] : <Widget>[menu],
      );

  /// 仅给收藏界面用
  static AppBar collect(
    BuildContext context,
    String leftText,
    GestureTapCallback left,
  ) =>
      AppBar(
        centerTitle: true,
        elevation: appbar_elevation,
        titleSpacing: 0.0,
        brightness: Brightness.light,
        backgroundColor: Colors.white,
        leading: Offstage(
          offstage: !Configs.isLogined,
          child: GestureDetector(
            onTap: left,
            child: Container(
              padding: inner_image_padding,
              child: Center(
                child: Text(
                  '$leftText',
                  style:
                      TextStyle(color: Colors.grey, fontSize: menu_font_size),
                ),
              ),
            ),
          ),
        ),
        title: Container(
          child: Image.asset(
            'imgs/js_navbars_img_navlogo.png',
            fit: BoxFit.scaleDown,
          ),
          padding: inner_image_title_padding,
        ),
        actions: <Widget>[
          GestureDetector(
            onTap: () {
              Navigator.pushNamed(context, '/cart');
            },
            child: Container(
              padding: inner_image_padding,
              child: Image.asset(
                Configs.cartCount == 0
                    ? 'imgs/js_navbars_img_shoppingcart_empty.png'
                    : 'imgs/js_navbars_img_shoppingcart_exist.png',
                fit: BoxFit.scaleDown,
              ),
            ),
          ),
        ],
      );

  /// 仅给账户界面用
  static AppBar account(BuildContext context) => AppBar(
        centerTitle: true,
        elevation: appbar_elevation,
        titleSpacing: 0.0,
        brightness: Brightness.light,
        backgroundColor: Colors.white,
        leading: GestureDetector(
          onTap: () {
            Navigator.pushNamed(context, '/message');
          },
          child: Container(
            padding: inner_image_padding,
            child: Image.asset(
              'imgs/js_navbars_img_btn_livechat_n.png',
              fit: BoxFit.scaleDown,
            ),
          ),
        ),
        title: Container(
          child: Image.asset(
            'imgs/js_navbars_img_navlogo.png',
            fit: BoxFit.scaleDown,
          ),
          padding: inner_image_title_padding,
        ),
        actions: !Configs.isLogined
            ? <Widget>[
                GestureDetector(
                  onTap: () {
                    Navigator.pushNamed(context, '/notification');
                  },
                  child: Container(
                    padding: inner_image_padding,
                    child: Image.asset(
                      'imgs/js_tabs_profile_img_btn_notify_n.png',
                      fit: BoxFit.scaleDown,
                    ),
                  ),
                ),
              ]
            : const <Widget>[],
      );

  static AppBar searchable(
    BuildContext context, {
    TextEditingController textController,
    VoidCallback edittingComplete,
    String hint = '搜索',
  }) {
    return AppBar(
      elevation: appbar_elevation,
      titleSpacing: 0.0,
      brightness: Brightness.light,
      backgroundColor: Colors.white,
      automaticallyImplyLeading: false,
      actions: <Widget>[
        GestureDetector(
          child: Container(
            child: Center(
              child: Text(
                '取消',
                style: TextStyle(color: Configs.main),
              ),
            ),
            color: Colors.white,
            padding: EdgeInsets.symmetric(horizontal: 10.0, vertical: 5.0),
          ),
          onTap: () {
            Navigator.maybePop(context);
          },
        ),
      ],
      title: Container(
        decoration: ShapeDecoration(
          shape:
              RoundedRectangleBorder(borderRadius: BorderRadius.circular(15.0)),
          color: Configs.background,
        ),
        child: TextField(
          keyboardType: TextInputType.text,
          textInputAction: TextInputAction.search,
          decoration: InputDecoration(
              prefixIcon: Icon(Icons.search),
              hintText: '$hint',
              border: InputBorder.none),
          controller: textController,
          onEditingComplete: edittingComplete,
          maxLines: 1,
        ),
        margin: EdgeInsets.symmetric(vertical: 5.0, horizontal: 5.0),
      ),
    );
  }
}
